package com.example.assignment01_javaee.Service.Impl;

import com.example.assignment01_javaee.Dao.ProductDao;
import com.example.assignment01_javaee.Entity.Inventory;
import com.example.assignment01_javaee.Dao.InventoryDao;
import com.example.assignment01_javaee.Entity.Product;
import com.example.assignment01_javaee.Service.InventoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class InventoryServiceImpl implements InventoryService {
    @Autowired
    public InventoryDao inventoryDao;
    @Autowired
    public ProductDao productDao;
    //查询所有的inventory
    @Override
    public List<Inventory> getAllInventories(){
        return inventoryDao.getAllInventories();
    }

    //根据product id查找inventory
    @Override
    public Inventory getInventoryByProductId(int productId){

        return inventoryDao.getInventoryByProductId(productId);
    }

    //更改inventory数据
    @Override
    public String updateInventoryById(int productId,int quantity){
        Map<String,Integer> map = new HashMap<>();
        Product product = productDao.getProductById(productId);
        if(product != null){
            map.put("productId",productId);
            map.put("quantity",quantity);

            inventoryDao.updateInventoryById(map);
            return "更新成功！";
        }
        return "该商品不存在！";
    }
}
